package com.example.lj.uav.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.lj.uav.R;

import dji.common.airlink.ChannelInterference;

public class ChannelLayout extends FrameLayout {

    private View view;
    private Context context;
    private ChannelView channel;
    private DragRectView drag;

    public ChannelLayout(@NonNull Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public ChannelLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public ChannelLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.layout_channel, this);
        channel = findViewById(R.id.channel_channel);

    }

    private float max = 0;
    private float min = 0;

    public void setupValue(float[] cnl, float[] power) {

        for (int i = 0; i < cnl.length; i++) {
            max = max > cnl[i] ? max : cnl[i];
            min = min < cnl[i] ? min : cnl[i];
        }

        channel.setupCoordinatorX("MHz", "dBm/MHz", cnl);
        channel.setupCoordinatorY(-100, -60);
        channel.addWave(ContextCompat.getColor(context, R.color.springgreen), false, power);
        channel.post(new Runnable() {
            @Override
            public void run() {
                int l = channel.getCoorLeft();
                int r = channel.getCoorRight();
                int t = channel.getCoorTop();
                int b = channel.getCoorBottom();
                drag = findViewById(R.id.channel_drag);
                ViewGroup.LayoutParams params = drag.getLayoutParams();
                params.width = r;
                params.height = b;
                drag.setLayoutParams(params);

                int[] locations = new int[2];
                ChannelLayout.this.getLocationInWindow(locations);
                drag.initRect(l, r, t, b, locations[0], locations[1]);
                drag.setVisibility(VISIBLE);
                drag.setOnActionUpListener(new DragRectView.OnActionUpListener() {
                    @Override
                    public void onActionUp(float p) {
                        channelListener.OnChannelSelected((int) (p * (max - min)));
                    }
                });
            }
        });
    }

    private OnChannelSelectedListener channelListener;

    public interface OnChannelSelectedListener {
        void OnChannelSelected(int p);
    }

    public void setOnChannelSelectedListener(OnChannelSelectedListener cb) {
        channelListener = cb;
    }
}
